﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Xpom_Xpum__Integrated_Development_Environment.Model;

namespace Xpom_Xpum__Integrated_Development_Environment.View
{
    /// <summary>
    /// Interaction logic for ChooseTemplateWindowView.xaml
    /// </summary>
    public partial class ChooseTemplateWindowView : Window
    {
        public ChooseTemplateWindowView()
        {
            InitializeComponent();
            this.DataContext = ((App)Application.Current).ctwvm;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();

            /*Directory.CreateDirectory(textBox2.Text);

            var d = "Configuration/Templates/" + (listView1.SelectedItem as Template).TemplateDirectory + (listView1.SelectedItem as Template).TemplateFilesDirectory;
            foreach (var srcPath in Directory.GetFiles(d))
            {
                File.Copy(srcPath, srcPath.Replace(d, textBox2.Text), true);
            }*/

            var z = "create \"" + textBox2.Text + "\" \"" + (listView1.SelectedItem as Template).TemplateNameEN + "\"";
            byte[] utf16Bytes = Encoding.Unicode.GetBytes(z);
            byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding(866), utf16Bytes);
            ((App)Application.Current).XCGProcess.StandardInput.BaseStream.Write(utf8Bytes, 0, utf8Bytes.Length);
            ((App)Application.Current).XCGProcess.StandardInput.WriteLine();

            System.Threading.Thread.Sleep(1000);

            ((App)App.Current).pevm = new ViewModel.Controls.ProjectExplorerViewModel(textBox2.Text);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Visibility = Visibility.Hidden;
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textBox2 == null)
                return;

            if (!String.IsNullOrEmpty(textBox1.Text))
            {
                if (textBox2.Text[textBox2.Text.Length - 1] != '\\')
                {
                    textBox2.Text += '\\';
                }

                var z = textBox2.Text.Split('\\');
                z[z.Length-2] = textBox1.Text;
                for (int i = 0; i < z.Length - 1; i++)
                {
                    z[i] += "\\";
                }
                textBox2.Text = String.Concat(z);
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var fbd = new System.Windows.Forms.FolderBrowserDialog();
            
            var result = fbd.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                string projectPath = fbd.SelectedPath;

                var z = textBox2.Text.Split('\\');

                textBox2.Text = projectPath + '\\' + z[z.Length - 2] + '\\';

                textBox2.Text = textBox2.Text.Replace("\\\\", "\\");
            }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
        }
    }
}
